from flask import Flask

class Application(object):

    # 共享app
    app = Flask(__name__)

    def __init__(self) -> None:
        super().__init__()


    def setup(self):
        """在运行程序前进行设置，例如连接数据库等等"""
        pass

    def abandon(self):
        """在运行程序后进行设置，例如终止与数据库的连接等等"""
        pass
    
    # @staticmethod
    # @app.route("/test")
    # def hello_world():
    #     return "<h1 align=\"center\">hello world!</h1>"

    
    def start_app(self, function=None):
        try:
            print("Starting ...")
            self.setup()

            # 主程序
            # function()

            self.app.run()
        except KeyboardInterrupt:
            print("Stoping ...")
            self.abandon()
            print("Done !")
        except Exception as unexcepted_error:
            print(f"Unexcepted_error: {unexcepted_error} happened")